#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using xConnected.Core.Interfaces.Service;
using xConnected.Core.Model;
using xConnected.Core.Model.ViewModel;

#endregion

namespace xConnected.Controllers
{
    public class PositionListController : ApiController

    {
        protected IPositionListService _positionListService;
        protected IPositionService _positionService;
        protected IUserService _userService;

        public PositionListController(IPositionListService positionListService, IUserService userService,
                                      IPositionService positionService)
        {
            _positionListService = positionListService;
            _userService = userService;
            _positionService = positionService;
        }

        public IEnumerable<PositionList> List()
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;

                var positionLists =
                    _positionListService.GetAll().Where(p => p.OwnerId == userId && p.IsBlackList == false).OrderBy(
                        p => p.Id);

                return positionLists;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        public HttpResponseMessage PostPositionList(PositionList positionList)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var user = _userService.GetUserByUsername(User.Identity.Name);
                var foundList =
                    _positionListService.GetAll().Where(m => m.OwnerId == user.Id && m.Name == positionList.Name);
                if (!foundList.Any())
                {
                    positionList.OwnerId = user.Id;
                    positionList.IsBlackList = false;

                    _positionListService.SaveOrUpdate(positionList);

                    return new HttpResponseMessage(HttpStatusCode.Created);
                }
                return new HttpResponseMessage(HttpStatusCode.Forbidden);
            }

            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        public HttpResponseMessage AddPosition(BookmarkModel bookmarkModel)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;

                if (!bookmarkModel.IsBlock)
                {
                    var positionList = _positionListService.GetById(bookmarkModel.PositionList.Id);
                    var position = _positionService.GetById(bookmarkModel.Position.Id);
                    if (!positionList.Positions.Select(e => e.Id).Contains(position.Id))
                    {
                        positionList.Positions.Add(position);
                        _positionListService.SaveOrUpdate(positionList);
                    }
                }
                else
                {
                    var blockList = _positionListService.GetAll().First(e => e.IsBlackList && e.OwnerId == userId);
                    if ((blockList.Positions.Count != 0 &&
                         !blockList.Positions.Select(e => e.Id).Contains(bookmarkModel.ExpertProfile.Id)) ||
                        blockList.Positions.Count == 0)
                    {
                        blockList.Positions.Add(bookmarkModel.Position);
                        _positionListService.SaveOrUpdate(blockList);
                    }
                }

                return new HttpResponseMessage(HttpStatusCode.OK);
            }

            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
    }
}